//
//  TiDanDetailMapVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/9/11.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "TiDanDetailMapVC.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "LJAnnotation.h"
#import "LJAnnotationView.h"

@interface TiDanDetailMapVC ()<MKMapViewDelegate>

@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) UIButton *backBtn;

@end

@implementation TiDanDetailMapVC

-(UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, StatusBarHeight + 7, 44, 44)];
        [_backBtn setImage:[UIImage imageNamed:@"icon_map_back"] forState:0];
        [_backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (void)backClick {
    [self.rt_navigationController popViewControllerAnimated:YES];
}

-(MKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        _mapView.delegate = self;
    }
    return _mapView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.backBtn];
    [self.list enumerateObjectsUsingBlock:^(TiDanDetailPlanModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        LJAnnotation *tiAn = [LJAnnotation new];
        tiAn.type = 1;
        tiAn.num = idx + 1;
        tiAn.coordinate = CLLocationCoordinate2DMake([obj.delivery_lat doubleValue], [obj.delivery_lng doubleValue]);
        [self.mapView addAnnotation:tiAn];
        
        LJAnnotation *shouAn = [LJAnnotation new];
        shouAn.type = 2;
        shouAn.num = idx + 1;
        shouAn.coordinate = CLLocationCoordinate2DMake([obj.unload_lat doubleValue], [obj.unload_lng doubleValue]);
        [self.mapView addAnnotation:shouAn];
        
        CLLocationCoordinate2D arr[2];
        arr[0] = CLLocationCoordinate2DMake([obj.delivery_lat doubleValue], [obj.delivery_lng doubleValue]);
        arr[1] = CLLocationCoordinate2DMake([obj.unload_lat doubleValue], [obj.unload_lng doubleValue]);
        
        MKPolyline *cc = [MKPolyline polylineWithCoordinates:arr count:2];
        [self.mapView addOverlay:cc];
    }];
}

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        //如果有MKPloylineView的overlay,将其画出
        MKPolylineRenderer *lineView = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        lineView.strokeColor = HexColor(0x5780D9);
        lineView.lineWidth = 1;
        return lineView;
    }
    return nil;
}

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    // 自定义大头针
    if ([annotation isKindOfClass:[LJAnnotation class]]) {
        static NSString *pinID = @"LJAnnotation";
        LJAnnotationView *customPinView = (LJAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
        if (!customPinView) {
           customPinView = [[LJAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];
        }
        customPinView.annotation = (LJAnnotation *)annotation;
        return customPinView;
    }
    return nil;
}

@end
